package com.windliven.spoc.modules.init.config;

import com.google.common.collect.Lists;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @ProjectName: service-demo
 * @Auther: GERRY
 * @Date: 2018/11/21 21:43
 * @Description:
 */
 @SpringBootConfiguration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        // 创建基于url源
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 创建跨域配置对象
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许cookie跨域
        configuration.setAllowCredentials(true);
        // 允许跨域的方法
        configuration.setAllowedMethods(Lists.newArrayList("*"));
        configuration.setAllowedHeaders(Lists.newArrayList("*"));
        configuration.setAllowedOrigins(Lists.newArrayList("*"));
        configuration.setMaxAge(300L);
        // 注册跨域配置及路径
        source.registerCorsConfiguration("/**", configuration);

        return new CorsFilter(source);

    }
}
